#ifndef COMMON_DATA_H
#define COMMON_DATA_H

#include <QObject>
#include <memory>
#include <stdint.h>
#include <QString>
#include <QTimer>
#include <QSettings>
#include <QFile>
#include "g_config.h"
#include <QDebug>
#include <unistd.h>

#define GPIO_OUT_1_ENABLE "/sys/class/gpio/gpio24/value"            //气泵
#define GPIO_OUT_2_ENABLE "/sys/class/gpio/gpio23/value"            //检测设备

typedef enum{
    MAIN_RUN,               //运行
    MAIN_MAINTENANCE,       //维护
}MAIN_WORK_MODE_e;

typedef enum{
    MAIN_DETECTION,        //检测
    MAIN_CALIBRATION,      //标定
}MAIN_MENU_MODE_e;

typedef enum{
    MAIN_SINGLE_DETECTION,      //单次检测
    MAIN_LOOP_DETECTION,        //循环检测
    MAIN_TIMED_DETECTION,       //定时检测
    MAIN_SINGLE_CALIBRATION,    //单次标定
    MAIN_MULTIPLE_CALIBRATION   //多次标定
}MAIN_FUNC_MODE_e;

typedef struct {
    QString sys_time;
    QString sys_mode;
    QString message;
}LOG_INFO_T;

struct CALIB_DATA_T {
    int id;
    QString sys_time;
    double peak_area;
    double gas_value;

    // 可选：重载<运算符以便直接排序
    bool operator<(const CALIB_DATA_T &other) const {
        return peak_area < other.peak_area;
    }
};

typedef struct {
    int id;
    QString sys_time;  //QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")
    QString datatype;       //数据类型      检测  标定
    QString dataProperty;   //数据属性      运行模式  维护模式
    double peak_area;       //峰面积
    double peak_height;     //峰高
    double gas_concentration;   //气体浓度
    double RT;              //保留时间
    double oven_temperature;         //柱箱温度
    double carrier_gas;              //读取到的载气压力值
    double detector_temperature;     //检测器温度
    int data_size;
    QByteArray data;
}GAS_VALUE_INFO_T;

typedef struct{
    double oven_temperature;         //柱箱温度
    double sample_time;              //采样时间
    uint16_t reserve_time;          //保留时间
    double fluctuation_threshold;    //波动阈值
    uint16_t pid_lamp_voltage;      //PID灯电压
    double detector_temperature;     //检测器温度
    double carrier_gas;              //载气压力
    uint16_t offset_time;           //偏移时间
    uint16_t filter_window;         //滤波窗口
    uint16_t analysis_duration;     //分析时长
    QString deviceId;               //设备ID
    uint8_t gas_name_index;         //气体名称下标
    uint8_t gas_unit_index;         //气体单位下标
    uint8_t power_supply_index;     //供电方式下标
    uint8_t baud_index;             //波特率下标
    uint8_t detect_mode_index;      //检测模式下标
    uint8_t loop_detect_time;       //循环检测时间
    uint8_t detect_time_1_hour;     //定时检测1小时
    uint8_t detect_time_1_min;      //定时检测1小时
    bool detect_time_1_open_flag;   //定时检测1开启标志
    uint8_t detect_time_2_hour;     //定时检测2小时
    uint8_t detect_time_2_min;      //定时检测2小时
    bool detect_time_2_open_flag;   //定时检测2开启标志
    uint8_t detect_time_3_hour;     //定时检测3小时
    uint8_t detect_time_3_min;      //定时检测3小时
    bool detect_time_3_open_flag;   //定时检测3开启标志
    bool sleep_mode_open_flag;      //休眠模式开启标志
    QString dev_software_version;   //设备软件版本
    QString dev_hardware_version;   //设备硬件版本
}INIT_CONFIG_PARAM_T;

class common_data : public QObject
{
    Q_OBJECT
public:
    inline static MAIN_FUNC_MODE_e current_func_mode = MAIN_LOOP_DETECTION;       //当前工作模式
    inline static MAIN_WORK_MODE_e run_mt_mode = MAIN_RUN;                          //运行或维护模式
    inline static MAIN_MENU_MODE_e dt_calib_mode = MAIN_DETECTION;                  //检测或标定模式
    inline static bool gpio_out1_dev_open_flag = false;                             //气泵设备开启标识
    inline static bool gpio_out2_dev_open_flag = false;                             //检测设备开启标识
    inline static bool ttyUSB2_to_devinfo_flag = false;                             //4G模块获取IMEI号与CCID号，向设备信息发送标识

    //初始化参数，设备上电后，如果没有ini文件，则使用以下默认参数，并生成ini文件并保存
    inline static INIT_CONFIG_PARAM_T init_config_param =
    {
        .oven_temperature = 70.0,
        .sample_time = 55.0,
        .reserve_time = 100,
        .fluctuation_threshold = 0.1,
        .pid_lamp_voltage = 1500,
        .detector_temperature = 40.0,
        .carrier_gas = 80.0,
        .offset_time = 20,
        .filter_window = 10,
        .analysis_duration = 200,
        .deviceId = "000000000001",
        .gas_name_index = 0,
        .gas_unit_index = 0,
        .power_supply_index = 0,
        .baud_index = 0,
        .detect_mode_index = 1,
        .loop_detect_time = 15,
        .detect_time_1_hour = 8,     //定时检测1小时
        .detect_time_1_min = 30,      //定时检测1小时
        .detect_time_1_open_flag = false,   //定时检测1开启标志
        .detect_time_2_hour = 13,     //定时检测2小时
        .detect_time_2_min = 30,      //定时检测2小时
        .detect_time_2_open_flag = false,   //定时检测2开启标志
        .detect_time_3_hour = 18,     //定时检测3小时
        .detect_time_3_min = 30,      //定时检测3小时
        .detect_time_3_open_flag = false,   //定时检测3开启标志
        .sleep_mode_open_flag = true,      //休眠模式开启标志
        .dev_software_version = "V1.01",
        .dev_hardware_version = "V1.01",
    };

    void gpio_enable_preheat(bool flag);        //GPIO控制，0：关闭继电器， 1： 打开继电器

signals:
    void common_data_to_uart5_signal(bool flag);        //向气泵发送信号， 0：发送报文关闭气泵  1： 发送报文打开气泵
};


uint16_t x_Crc16Check(const uint8_t *acu8I_Crc16Buf, uint16_t u16I_Crc16Len);


int sw_linux_cmd(char *cmd,char *back,int len);
void start_param_gpio_control();
void LoadAdjustParameter();
void gpio_dev_control();
void gpio_out1_dev_enable(uint8_t value);
void gpio_out2_dev_enable(uint8_t value);

#endif // COMMON_DATA_H

